//
//  MusicListViewController.m
//  OurRelax
//
//  Created by 王雅蓉 on 15/6/18.
//  Copyright (c) 2015年 于宾尧. All rights reserved.
//

#define Tablebar 80
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#import "MusicListViewController.h"
#import "MusicListTableViewCell.h"
#import "MusicDetailShareHandle.h"

@interface MusicListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) NSMutableArray *musicListArray;

@property (nonatomic, retain) UIButton *playButton;
@property (nonatomic, retain) UIButton *nextButton;
@property (nonatomic, retain) UIButton *lastButton;

@end

@implementation MusicListViewController

-(void)dealloc
{
    //[_listTableView release];
    //[_titleLabel release];
    
    [super dealloc];
}

#pragma 改
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //播放栏
    if ([TagHandle shareTagHandle].isPlaying) {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_pause.png"] forState:UIControlStateNormal];
    } else {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    }
    
}
#pragma 改
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //播放栏
    if ([TagHandle shareTagHandle].isPlaying) {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_pause.png"] forState:UIControlStateNormal];
    } else {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.musicListArray = [MusicDetailShareHandle shareListDataHandle].listShareArray;
    
    [self setUI];
    
    //[Player shareDataHandle].player.delegate = self;
    
}

-(void)setUI
{
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 50)];
    self.titleLabel.font = [UIFont systemFontOfSize:16.0];
    NSString *titleStr = [MusicDetailShareHandle shareListDataHandle].playingMusic.title;
    NSString *string = @"    正在播放：";
    if (titleStr.length != 0) {
        string = [NSString stringWithFormat:@"%@%@",string, titleStr];
    }
    self.titleLabel.text = string;
    [self.view addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, _titleLabel.frame.size.width, self.view.frame.size.height - _titleLabel.frame.size.height - 20 - 80)];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    [self.view addSubview:_listTableView];
    //[_listTableView release];
    
    // ************************* 播放栏 ******************************
    UIImageView *tableBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, ScreenHeight - Tablebar, ScreenWidth, Tablebar)];
    //tableBar.text = @"tableBar";
    //tableBar.backgroundColor = [UIColor yellowColor];
    tableBar.userInteractionEnabled = YES;
    tableBar.image = [UIImage imageNamed:@"img_music.png"];
    [self.view addSubview:tableBar];
    
    self.lastButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([[UIScreen mainScreen] bounds].size.width == 375) {
        
        self.lastButton.frame = CGRectMake(80, tableBar.frame.origin.y + 10, tableBar.frame.size.height - 20, tableBar.frame.size.height - 20);
        
    }  else if ([[UIScreen mainScreen] bounds].size.width > 375) {
    
        self.lastButton.frame = CGRectMake(110, tableBar.frame.origin.y + 10, tableBar.frame.size.height - 20, tableBar.frame.size.height - 20);
    } else {
    
        self.lastButton.frame = CGRectMake(ScreenWidth / 2.0 - ((tableBar.frame.size.height - 20) * 3) / 2.0, tableBar.frame.origin.y + 10, tableBar.frame.size.height - 20, tableBar.frame.size.height - 20);
    }
    //lastButton.backgroundColor = [UIColor lightGrayColor];
    [self.lastButton setBackgroundImage:[UIImage imageNamed:@"lastsong.png"] forState:UIControlStateNormal];
    [self.lastButton addTarget:self action:@selector(lastAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_lastButton];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([[UIScreen mainScreen] bounds].size.width >= 375) {
        
        self.playButton.frame = CGRectMake(_lastButton.frame.origin.x + _lastButton.frame.size.width + 15, _lastButton.frame.origin.y, _lastButton.frame.size.width, _lastButton.frame.size.height);
        
    } else {
    
        self.playButton.frame = CGRectMake(_lastButton.frame.origin.x + _lastButton.frame.size.width, _lastButton.frame.origin.y, _lastButton.frame.size.width, _lastButton.frame.size.height);
    }
    //playButton.backgroundColor = [UIColor lightGrayColor];
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    [self.playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playButton];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([[UIScreen mainScreen] bounds].size.width >= 375) {
        
        self.nextButton.frame = CGRectMake(_playButton.frame.origin.x + _playButton.frame.size.width + 10, _playButton.frame.origin.y, _playButton.frame.size.width, _playButton.frame.size.height);
        
    } else {
    
        self.nextButton.frame = CGRectMake(_playButton.frame.origin.x + _playButton.frame.size.width, _playButton.frame.origin.y, _playButton.frame.size.width, _playButton.frame.size.height);
    }
    //nextButton.backgroundColor = [UIColor lightGrayColor];
    [_nextButton setBackgroundImage:[UIImage imageNamed:@"nextsong.png"] forState:UIControlStateNormal];
    [_nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextButton];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(ScreenWidth - tableBar.frame.size.height + 30, tableBar.frame.origin.y + 30, tableBar.frame.size.height - 60, tableBar.frame.size.height - 60);
    //backButton.backgroundColor = [UIColor lightGrayColor];
    [backButton setBackgroundImage:[UIImage imageNamed:@"iconfont-cha.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

//上一曲
-(void)lastAction:(UIButton *)button
{
    [[Player shareDataHandle] lastPlay];
}

//播放
-(void)playAction:(UIButton *)button
{
    [TagHandle shareTagHandle].isPlaying = ![TagHandle shareTagHandle].isPlaying;
    
    if ([TagHandle shareTagHandle].isPlaying) {
#pragma 改
        if ([Player shareDataHandle].oldIndex != [Player shareDataHandle].playingIndex) {
            
            [[Player shareDataHandle] PlayForArray];
        } else {
            [[Player shareDataHandle].player resume];
        }
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_pause.png"] forState:UIControlStateNormal];
        
    } else {
            
        [[Player shareDataHandle].player pause];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    }
}

//下一曲
-(void)nextAction:(UIButton *)button
{
    [[Player shareDataHandle] NextPlay];
}

//回撤
-(void)backButton:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicForSonglist *tmpSonglist = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:indexPath.row];
    [MusicDetailShareHandle shareListDataHandle].statusMusic = tmpSonglist;
    
#pragma 改
    [Player shareDataHandle].playingIndex = indexPath.row;
    [Player shareDataHandle].currentUrlArray = [MusicDetailShareHandle shareListDataHandle].listURLArray;
    [[Player shareDataHandle] PlayForArray];
    
    //播放栏
    if ([TagHandle shareTagHandle].isPlaying) {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_pause.png"] forState:UIControlStateNormal];
    } else {
        
        [_playButton setBackgroundImage:[UIImage imageNamed:@"play_bar_play.png"] forState:UIControlStateNormal];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musicListArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 80;
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *listCellIdentifier = @"listcell";
    
    MusicListTableViewCell *musicListCell = [tableView dequeueReusableCellWithIdentifier:listCellIdentifier];
    
    if (musicListCell == nil) {
        
        musicListCell = [[MusicListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listCellIdentifier];
    }
    
    musicListCell.listMusicDetail = [[MusicDetailShareHandle shareListDataHandle].listShareArray objectAtIndex:indexPath.row];
    
    return musicListCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
